package com.ruoyi.web.controller.business.order;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.business.order.domain.JbPaymentReceived;
import com.ruoyi.business.order.domain.vo.JbPaymentReceivedVO;
import com.ruoyi.business.order.service.JbPaymentReceivedService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.controller.business.order.factory.JbPaymentReceivedFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 回款 Controller
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Api("回款")
@RestController
@RequestMapping("/payment_received")
public class JbPaymentReceivedController extends BaseController
{
    @Autowired
    private JbPaymentReceivedService service;

    @Autowired
    private JbPaymentReceivedFactory factory;

    /**
     * 查询回款列表
     */
    @ApiOperation("查询回款列表")
    @PreAuthorize("@ss.hasPermi('payment_received:list')")
    @GetMapping("/list")
    public TableDataInfo list(PageParam page, JbPaymentReceivedVO jbPaymentReceivedVO)
    {
        return getDataTable(factory.list(page, jbPaymentReceivedVO));
    }

    /**
     * 导出回款列表
     */
    @ApiOperation("导出回款列表")
    @PreAuthorize("@ss.hasPermi('payment_received:export')")
    @Log(title = "回款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageParam page, JbPaymentReceived jbPaymentReceived)
    {
        LambdaQueryWrapper<JbPaymentReceived> select = new LambdaQueryWrapper<>();
        IPage<JbPaymentReceived> iPage = service.page(page.b(), select);

        List<JbPaymentReceived> list = iPage.getRecords();
        ExcelUtil<JbPaymentReceived> util = new ExcelUtil<JbPaymentReceived>(JbPaymentReceived.class);
        util.exportExcel(response, list, "回款数据");
    }

    /**
     * 获取回款详细信息
     */
    @ApiOperation("获取回款详细信息")
    @PreAuthorize("@ss.hasPermi('payment_received:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(factory.getById(id));
    }

    /**
     * 获取回款详细信息
     */
    @ApiOperation("获取回款详细信息")
    @PreAuthorize("@ss.hasPermi('payment_received:query')")
    @GetMapping(value = "/businessCode/{businessCode}")
    public AjaxResult businessCode(@PathVariable("businessCode") String businessCode)
    {
        JbPaymentReceived jbPaymentReceived = factory.getInfo(businessCode);
        if (ObjectUtil.isNotEmpty(jbPaymentReceived))
        {
            return success(factory.getById(jbPaymentReceived.getId()));
        }
        return error();
    }

    /**
     * 新增回款
     */
    @ApiOperation("新增回款")
    @PreAuthorize("@ss.hasPermi('payment_received:add')")
    @Log(title = "回款", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JbPaymentReceived jbPaymentReceived)
    {
        return toAjax(factory.add(jbPaymentReceived));
    }

    /**
     * 修改回款
     */
    @ApiOperation("修改回款")
    @PreAuthorize("@ss.hasPermi('payment_received:edit')")
    @Log(title = "回款", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JbPaymentReceived jbPaymentReceived)
    {
        return toAjax(factory.edit(jbPaymentReceived));
    }

    /**
     * 删除回款
     */
    @ApiOperation("删除回款")
    @PreAuthorize("@ss.hasPermi('payment_received:remove')")
    @Log(title = "回款", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(factory.remove(ids));
    }
}
